package tr.edu.metu.is.mega.server.business;

import java.sql.Timestamp;

import tr.edu.metu.is.mega.common.dto.UserDTO;
import tr.edu.metu.is.mega.common.dto.UserRole;
import tr.edu.metu.is.mega.server.core.AbstractDTOHelper;
import tr.edu.metu.is.mega.server.entity.Systemuser;

/**
 * Helper class to convert between DTO and entity.
 */
public class UserDTOHelper extends AbstractDTOHelper<UserDTO, Systemuser> {

    /**
     * {@inheritDoc}
     */
    @Override
    public UserDTO convertToDTO(Systemuser from) {
        UserDTO to = new UserDTO();
        to.setPkid(from.getPkid());
        to.setEmail(from.getEmail());
        to.setExpiryDate(from.getExpirydate());
        to.setIdentificationNo(from.getIdentificationno());
        to.setName(from.getName());
        to.setPassword(from.getPassword());
        to.setPhoto(from.getPhoto());
        to.setSurname(from.getSurname());
        to.setUserName(from.getUsername());
        to.setRole(UserRole.findByCode(from.getUserrole()));
        return to;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void convertToEntity(UserDTO from, Systemuser to) {
        to.setPkid(from.getPkid());
        to.setEmail(from.getEmail());
        if (from.getExpiryDate() != null) {
            to.setExpirydate(new Timestamp(from.getExpiryDate().getTime()));
        }
        to.setIdentificationno(from.getIdentificationNo());
        to.setName(from.getName());
        to.setPassword(from.getPassword());
        to.setPhoto(from.getPhoto());
        to.setSurname(from.getSurname());
        to.setUsername(from.getUserName());
        if (from.getRole() != null) {
            to.setUserrole(from.getRole().getCode());
        }
    }

}
